package com.sunl.mall.ali.mq.autoconfigure.scanner.producer;


import com.sunl.mall.ali.mq.autoconfigure.MqEnvProperties;
import com.sunl.mall.ali.mq.autoconfigure.util.RegUtil;
import com.sunl.mall.ali.mq.producer.RocketMqProducerBean;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.env.Environment;
import org.springframework.core.type.AnnotationMetadata;

/**
 * 获取properties创建RocketMqProducerBean
 */
@Slf4j
public class MqProducerBeanScannerRegistrar implements ImportBeanDefinitionRegistrar/*实现registrar同时使用@import注入该bean到spring*/, EnvironmentAware {
    private Environment environment;

    @Override
    public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
        log.debug("Searching for MqProducer from Environment");
        Class<?> beanClass = RocketMqProducerBean.class;
        RegUtil.registerRocketMqProducerBeans(registry, MqEnvProperties.MQ_PRODUCERS_PREFIX, beanClass, this.environment);
    }

    @Override
    public void setEnvironment(Environment environment) {
        this.environment = environment;
    }
}